<template>
  <div>
    <a-card :bordered="false" :bodyStyle="tstyle">
      <div class="table-page-search-wrapper">
        <a-form layout="inline">
          <a-row :gutter="48">
            <a-col :md="8" :sm="24">
              <a-form-item label="实验模块">
                <a-input v-model="queryParam.searchValue" allow-clear placeholder="请输入名称"/>
              </a-form-item>
            </a-col>
            <a-col :md="8" :sm="24">
              <a-form-item label="所属学院">
                <a-select v-model="queryParam.collId" allow-clear placeholder="请选择"
                          :disabled="collSwitch">
                  <a-select-option v-for="(item,index) in collList" :key="index" :value="item.id">{{ item.name }}
                  </a-select-option>
                </a-select>
              </a-form-item>
            </a-col>
            <a-col :md="8" :sm="24">
              <span class="table-page-search-submitButtons">
                <a-button type="primary" @click="$refs.table.refresh(true)">查询</a-button>
                <a-button style="margin-left: 8px" @click="resetSearch">重置</a-button>
              </span>
            </a-col>
          </a-row>
        </a-form>
      </div>
    </a-card>
    <a-card :bordered="false">
      <s-table
        ref="table"
        :columns="columns"
        :data="loadData"
        :alert="options.alert"
        :rowKey="(record) => record.id"
        :rowSelection="options.rowSelection"
      >
        <template class="table-operator" slot="operator">
          <a-button type="primary" icon="plus" @click="$refs.detail.open()">新增实验模块</a-button>
          <a-popconfirm title="确认批量删除？" @confirm="() => batchDelete()">
            <a-button type="danger" :disabled="selectedRowKeys.length < 1">
              <a-icon type="delete"/>
              批量删除
            </a-button>
          </a-popconfirm>
        </template>
        <span slot="status" slot-scope="text,record">
            <a-switch
              :loading="swtichLoading"
              checked-children="正常"
              un-checked-children="停用"
              :defaultChecked="text===0"
              @click="()=>editUserStatus(text,record)"/>
        </span>
        <span slot="action" slot-scope="text, record">
          <a @click="$refs.detail.open(record)">编辑</a>
          <a-divider type="vertical"/>
          <a-popconfirm placement="topRight" title="确认删除？" @confirm="() => singleDelete(record)">
            <a>删除</a>
          </a-popconfirm>
        </span>
      </s-table>
    </a-card>
    <module-detail ref="detail" @ok="handleOk"/>
  </div>
</template>
<script>
  import { STable, XDown } from '@/components'
  import ModuleDetail from './detail'
  import store from '@/store'
  import { sysCollegeList } from '@/api/modular/system/sysCollegeManage'
  import { moduleChangeStatus, moduleDelete, modulePage } from '@/api/modular/system/moduleManage'

  export default {
    components: {
      STable,
      XDown,
      ModuleDetail
    },
    data() {
      return {
        // 查询参数
        queryParam: {
          collId: undefined
        },
        // 表头
        columns: [
          {
            title: '所属学院',
            dataIndex: 'collName'
          },
          {
            title: '实验模块',
            align: 'center',
            dataIndex: 'name'
          },
          {
            title: '排序号',
            align: 'center',
            dataIndex: 'sortNum'
          },
          {
            title: '实验模块说明',
            align: 'center',
            ellipsis: true,
            dataIndex: 'remark'
          },
          {
            title: '状态',
            dataIndex: 'status',
            scopedSlots: { customRender: 'status' }
          }
        ],
        tstyle: { 'padding-bottom': '0px', 'margin-bottom': '10px' },
        // 加载数据方法 必须为 Promise 对象
        loadData: parameter => {
          return modulePage(Object.assign(parameter, this.queryParam)).then((res) => {
            return res.data
          })
        },
        selectedRowKeys: [],
        selectedRows: [],
        options: {
          alert: {
            show: true, clear: () => {
              this.selectedRowKeys = []
            }
          },
          rowSelection: {
            selectedRowKeys: this.selectedRowKeys,
            onChange: this.onSelectChange
          }
        },
        swtichLoading: false,
        collList: [],
        collSwitch: store.getters.admintype != 1
      }
    },
    created() {
      this.columns.push({
        title: '操作',
        width: '150px',
        dataIndex: 'action',
        scopedSlots: { customRender: 'action' }
      })
    },
    mounted() {
      this.getCollList()
      this.queryParam.collId = store.getters.admintype != 1 ? store.getters.userInfo.collId : undefined
    },
    methods: {
      resetSearch(){
        this.queryParam.searchValue=undefined;
        if(store.getters.admintype === 1){
          this.queryParam.collId=undefined;
        }
      },
      getCollList() {
        sysCollegeList().then((res) => {
          if (res.success) {
            this.collList = res.data
          }
        })
      },
      /**
       * 修改项目状态
       */
      editUserStatus(code, record) {
        // eslint-disable-next-line no-unused-vars
        let status = 0
        let action = ''
        // eslint-disable-next-line eqeqeq
        if (code == 0) {
          status = 1
          action = '停用'
          // eslint-disable-next-line eqeqeq
        } else if (code == 1) {
          status = 0
          action = '启用'
        }
        this.swtichLoading = true
        moduleChangeStatus({ id: record.id, status: status }).then(res => {
          if (res.success) {
            this.$message.success(action + '操作成功')
            this.$refs.table.refresh()
          } else {
            this.$message.error(action + '操作失败：' + res.message)
          }
        }).finally(() => {
          this.swtichLoading = false
          this.$refs.table.refresh()
        })
      },
      /**
       * 单个删除
       */
      singleDelete(record) {
        const param = [{ 'id': record.id }]
        this.moduleDelete(param)
      },
      /**
       * 批量删除
       */
      batchDelete() {
        const paramIds = this.selectedRowKeys.map((d) => {
          return { 'id': d }
        })
        this.moduleDelete(paramIds)
      },
      moduleDelete(record) {
        moduleDelete(record).then((res) => {
          if (res.success) {
            this.$message.success('删除成功')
            this.$refs.table.clearRefreshSelected()
          } else {
            this.$message.error('删除失败') // + res.message
          }
        })
      },
      handleOk() {
        this.$refs.table.refresh()
      },
      onSelectChange(selectedRowKeys, selectedRows) {
        this.selectedRowKeys = selectedRowKeys
        this.selectedRows = selectedRows
      }
    }
  }
</script>
<style lang="less">
  .table-operator {
    margin-bottom: 18px;
  }

  button {
    margin-right: 8px;
  }
</style>
